<?php

namespace common\logics;

use Yii;
use common\helpers\Url;
use common\helpers\ColorHelper;
use common\logics\MapFenceLogic;

/**
 * MemberLogic is the member base model class.
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class MemberLogic extends \common\models\member\Member {

    /**
     * 获取会员信息
     * @return type
     */
    public function getTheme() {
        $levelIcon = $this->level ? $this->level->levelIcon : '';
        $avatarFrame = '';
        $nameColor = ColorHelper::format('#ff6600');
        if ($this->type) {
            $avatarFrame = $this->type->avatarFrame;
            $nameColor = $this->type->nameColor;
        }
        return [
            'level_icon' => $levelIcon,
            'avatar_frame' => $avatarFrame,
            'username_color' => $nameColor,
        ];
    }

    public function getStats() {
        $account = $this->account;
        return [
            'brokerage' => number_format($account ? $account->current : 0),
            'orders' => $this->orderCount,
            'withdraws' => $this->withdrawCount,
            'downers' => $this->downerCount,
        ];
    }

    /**
     * 获取分销中心菜单
     * @return type
     */
    public function getMenus() {
        $stats = $this->getCacheAttributes('stats');
        return [
            [
                'title' => '分销佣金',
                'icon' => Url::attach('/assets/icons/commission/brokerage.png'),
                'text' => [
                    'number' => $stats['brokerage'],
                    'unit' => '元',
                ],
                'link' => Url::weblink('commission/brokerage'),
            ],
            [
                'title' => '分销订单',
                'icon' => Url::attach('/assets/icons/commission/order.png'),
                'text' => [
                    'number' => $stats['orders'],
                    'unit' => '笔',
                ],
                'link' => Url::weblink('commission/order'),
            ],
            [
                'title' => '提现明细',
                'icon' => Url::attach('/assets/icons/commission/history.png'),
                'text' => [
                    'number' => $stats['withdraws'],
                    'unit' => '笔',
                ],
                'link' => Url::weblink('commission/history'),
            ],
            [
                'title' => '我的关注',
                'icon' => Url::attach('/assets/icons/commission/downer.png'),
                'text' => [
                    'number' => $stats['downers'],
                    'unit' => '人',
                ],
                'link' => Url::weblink('commission/downer'),
            ],
            [
                'title' => '佣金排名',
                'icon' => Url::attach('/assets/icons/commission/rank.png'),
                'text' => null,
                'link' => Url::weblink('commission/rank'),
            ],
            [
                'title' => '推广二维码',
                'icon' => Url::attach('/assets/icons/commission/qrcode.png'),
                'text' => null,
                'link' => Url::weblink('commission/qrcode'),
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'user',
            'type',
            'level',
            'scale',
            'account',
            'experience' => function() {
                return number_format($this->experience / 100);
            },
            'expired_at',
            'qrcode' => 'qrcodeUrl',
            'theme',
            'menus',
        ];
    }

}
